function c = nchoosek(v, k)
  n = length(v);
  v = v(:)';      //v is a row vector
  if k < 0 | int(k) ~= k 
    error('input number must be positive integers');
  end
  if n < k
    //c = [];
    //return
    error('size of v must be larger or equal to k');
  end
  
  if k == 1
    c = v(:);
    return
  end
  if k == 0
    c = [];
    return
  end
  
  c = [];
  for i = 1:n-k+1   //first element
    ck_1 = nchoosek(v(i+1:$), k-1);
    c = [c;
        v(i)*ones(size(ck_1,1),1) ck_1];
  end
endfunction
    
